package Euler34;

import java.util.*;
import ReusableCode.*;

public class DigitFactorials {
	public static void main(String[] args) {
		int fac[] = new int[10];
		int sum = 0;
		
		//Get the value of all factorials from 1 to 9.
		for(int i = 0; i < fac.length; i++)
		{
			fac[i] = MathFunctions.safeLongToInt(MathFunctions.getFactorial(i));
		}
		
		
		for(int i = 3; i < 2540160; i++)
		{
			int digsum = 0;
			int digits[] = MathFunctions.getDigits(i);
			
			for(int j = 0; j < digits.length; j++)
			{
				digsum += fac[digits[j]];
			}
			
			if(digsum == i)
			{
				sum += digsum;
				System.out.println(i);
			}
		}
		
		System.out.println("Total sum is: " + sum);
	}
}
